import moment from "moment";

const timeData = () => [
  { key: "0", value: "0:00" },
  { key: "1", value: "1:00" },
  { key: "2", value: "2:00" },
  { key: "3", value: "3:00" },
  { key: "4", value: "4:00" },
  { key: "5", value: "5:00" },
  { key: "6", value: "6:00" },
  { key: "7", value: "7:00" },
  { key: "8", value: "8:00" },
  { key: "9", value: "9:00" },
  { key: "10", value: "10:00" },
  { key: "11", value: "11:00" },
  { key: "12", value: "12:00" },
  { key: "13", value: "13:00" },
  { key: "14", value: "14:00" },
  { key: "15", value: "15:00" },
  { key: "16", value: "16:00" },
  { key: "17", value: "17:00" },
  { key: "18", value: "18:00" },
  { key: "19", value: "19:00" },
  { key: "20", value: "20:00" },
  { key: "21", value: "21:00" },
  { key: "22", value: "22:00" },
  { key: "23", value: "23:00" }
];

const setPushDep = detailPlan => {
  if (detailPlan && detailPlan.pushOrgIds && detailPlan.pushOrgIds !== "null") {
    const pushDep = [];
    const depData = [];
    JSON.parse(detailPlan.pushOrgIds).forEach(item => {
      const tm = { value: item.key, name: item.name };
      const tmp = { value: item.key, label: item.name };
      pushDep.push(tm);
      depData.push(tmp);
    });
    return { pushDep, depData };
  }
  return {};
};

const setPushPep = detailPlan => {
  if (
    detailPlan &&
    detailPlan.pushUserIds &&
    detailPlan.pushUserIds !== "null"
  ) {
    const people = [];
    const peopleName = [];
    JSON.parse(detailPlan.pushUserIds).forEach(item => {
      people.push(item.key);
      peopleName.push(item.name);
    });
    return {
      pushPep: JSON.parse(detailPlan.pushUserIds),
      people,
      peopleName,
      selectKey: JSON.parse(detailPlan.pushUserIds)
    };
  }
  return {};
};

const setTargetType = detailPlan => {
  if (
    detailPlan &&
    detailPlan.pushUserIds &&
    detailPlan.pushUserIds !== null &&
    detailPlan.pushUserIds !== "null" &&
    detailPlan.pushUserIds !== "" &&
    detailPlan.pushUserIds.length > 0
  ) {
    return 1;
  }
  if (
    detailPlan &&
    detailPlan.pushOrgIds &&
    detailPlan.pushOrgIds !== null &&
    detailPlan.pushOrgIds !== "null" &&
    detailPlan.pushOrgIds !== "" &&
    detailPlan.pushOrgIds.length > 0
  ) {
    return 2;
  }
  return 3;
};

const dayData = () => [
  "1",
  "2",
  "3",
  "4",
  "5",
  "6",
  "7",
  "8",
  "9",
  "10",
  "11",
  "12",
  "13",
  "14",
  "15",
  "16",
  "17",
  "18",
  "19",
  "20",
  "21",
  "22",
  "23",
  "24",
  "25",
  "26",
  "27",
  "28",
  "29",
  "30",
  "31"
];

const weekData = () => [
  { value: "1", title: "周一" },
  { value: "2", title: "周二" },
  { value: "3", title: "周三" },
  { value: "4", title: "周四" },
  { value: "5", title: "周五" },
  { value: "6", title: "周六" },
  { value: "7", title: "周日" }
];

const rennderValue = value => {
  if (value === 1) {
    return "智推公众号";
  }
  if (value === 2) {
    return "全科医生";
  }
  if (value === 3) {
    return "智能搜索";
  }
  return "";
};

const setWork = detailPlan => {
  if (detailPlan && detailPlan !== {}) {
    const startTime = moment(detailPlan.startTime).format(
      "YYYY-MM-DD HH:mm:ss"
    );
    const endTime = moment(detailPlan.endTime).format("YYYY-MM-DD HH:mm:ss");
    if (
      moment(endTime, "YYYY-MM-DD HH:mm:ss").diff(
        moment(startTime, "YYYY-MM-DD HH:mm:ss"),
        "years"
      ) >= 100
    ) {
      return { workTime: 1 };
    }
    if (
      moment(endTime, "YYYY-MM-DD HH:mm:ss").diff(
        moment(startTime, "YYYY-MM-DD HH:mm:ss"),
        "weeks"
      ) === 1
    ) {
      return { workTime: 4 };
    }
    if (
      moment(endTime, "YYYY-MM-DD HH:mm:ss").diff(
        moment(startTime, "YYYY-MM-DD HH:mm:ss"),
        "years"
      ) === 1
    ) {
      return { workTime: 2 };
    }
    if (
      moment(endTime, "YYYY-MM-DD HH:mm:ss").diff(
        moment(startTime, "YYYY-MM-DD HH:mm:ss"),
        "days"
      ) < 1
    ) {
      return { workTime: 5 };
    }
    if (
      moment(endTime, "YYYY-MM-DD HH:mm:ss").diff(
        moment(startTime, "YYYY-MM-DD HH:mm:ss"),
        "months"
      ) === 1
    ) {
      return { workTime: 3 };
    }
  }
  return {
    workTime: 6,
    defaultTime: [moment(detailPlan.startTime), moment(detailPlan.endTime)]
  };
};

const setFreq = detailPlan => {
  if (detailPlan && detailPlan.cronContent && detailPlan.cronContent !== "{}") {
    const tp = JSON.parse(detailPlan.cronContent);
    if (detailPlan && tp.startHour) {
      return {
        startHour: tp.startHour,
        endHour: tp.endHour,
        hourGap: tp.timeIntervel,
        freqType: "hour1",
        TabData: "1"
      };
    }
    if (detailPlan && tp.designatedHour) {
      const tmp = tp.designatedHour.split(",");
      return { SelfHour: tmp, freqType: "hour2", TabData: "1" };
    }
    if (detailPlan && tp.designatedDay && tp.frequencyType === 2) {
      const tmp = tp.designatedDay.split(",");
      const temp = tp.designatedDayHour.split(",");
      return { day: tmp, dayHour2: temp, freqType: "day2", TabData: "2" };
    }
    if (detailPlan && tp.designatedDayHour && tp.frequencyType === 2) {
      const tmp = tp.designatedDayHour.split(",");
      return { dayHour1: tmp, freqType: "day1", TabData: "2" };
    }
    const tmp = tp.designatedWeek.split(",");
    const temp = tp.designatedWeekHour.split(",");
    return { week: tmp, weekHour: temp, freqType: "week", TabData: "3" };
  }
  return {};
};

const freqVisible = detailPlan => {
  if (detailPlan.pushTriggerType === 0) {
    return { setTime: false };
  }
  return { setTime: true };
};

const workTimeClassify = (workTime, startTime, endTime) => {
  if (workTime === 1) {
    return {
      startTime: moment().format("YYYY-MM-DD HH:mm:ss"),
      endTime: moment()
        .add(101, "years")
        .format("YYYY-MM-DD HH:mm:ss")
    };
  }
  if (workTime === 2) {
    return {
      startTime: moment().format("YYYY-MM-DD HH:mm:ss"),
      endTime: moment()
        .add(1, "years")
        .format("YYYY-MM-DD HH:mm:ss")
    };
  }
  if (workTime === 3) {
    return {
      startTime: moment().format("YYYY-MM-DD HH:mm:ss"),
      endTime: moment()
        .add(1, "months")
        .format("YYYY-MM-DD HH:mm:ss")
    };
  }
  if (workTime === 4) {
    return {
      startTime: moment().format("YYYY-MM-DD HH:mm:ss"),
      endTime: moment()
        .add(1, "weeks")
        .format("YYYY-MM-DD HH:mm:ss")
    };
  }
  if (workTime === 5) {
    return {
      startTime: moment().format("YYYY-MM-DD HH:mm:ss"),
      endTime: moment()
        .format("YYYY-MM-DD")
        .concat(" 23:59:59")
    };
  }
  return { startTime, endTime };
};

const setTimeParam = (
  freqType,
  startHour,
  endHour,
  hourGap,
  SelfHour,
  dayHour1,
  dayHour2,
  day,
  week,
  weekHour,
  setTime
) => {
  if (setTime && freqType === "hour1") {
    return {
      frequencyType: 1,
      startHour,
      endHour,
      timeIntervel: hourGap
    };
  }
  if (setTime && freqType === "hour2") {
    return { frequencyType: 1, designatedHour: SelfHour.join(",") };
  }
  if (setTime && freqType === "day1") {
    return { frequencyType: 2, designatedDayHour: dayHour1.join(",") };
  }
  if (setTime && freqType === "day2") {
    return {
      frequencyType: 2,
      designatedDayHour: dayHour2.join(","),
      designatedDay: day.join(",")
    };
  }
  if (setTime && freqType === "week") {
    return {
      frequencyType: 3,
      designatedWeek: week.join(","),
      designatedWeekHour: weekHour.join(",")
    };
  }
  return {};
};

const setTargetParam = (type, pushPep, pushDep) => {
  if (type === 1) {
    return { pushUserIds: pushPep, pushOrgIds: [] };
  }
  if (type === 2) {
    return { pushOrgIds: pushDep, pushUserIds: [] };
  }
  return { pushOrgIds: [], pushUserIds: [] };
};

const styleBtn = {
  position: "fixed",
  backgroundColor: "#fff",
  bottom: 0,
  marginLeft: -16,
  display: "flex",
  justifyContent: "flex-end",
  height: 64,
  alignItems: "center",
  boxShadow: "0px -1px 2px 0px rgba(0, 0, 0, 0.03)",
  borderTop: "1px solid #e8e8e8",
  zIndex: 500
};

export default {
  timeData,
  dayData,
  weekData,
  setTargetType,
  rennderValue,
  setWork,
  setFreq,
  freqVisible,
  setPushDep,
  setPushPep,
  workTimeClassify,
  setTimeParam,
  setTargetParam,
  styleBtn
};
